package com.bemes.ecmn.model;

public class ECMNConnector extends ECMNGenericObject{

	private ECMNCompositionObject m_parent;
	private ECMNLinkableObject m_child;
	
	private int m_PGranularity;
	private int m_CGranularity;
	
	public ECMNConnector()
	{
		m_PGranularity = m_CGranularity = 1;
	}
	
	public boolean makeLink(ECMNCompositionObject parent, ECMNLinkableObject child)
	{
		if(parent == null || child == null) // check for null pointer
		{
			return false;
		}
		else
		{
			m_parent = parent;
			m_child = child;
			m_parent.AddConnector(this);
			return true;
		}
	}
	
	public boolean SetGranularity(int parentGranularity, int childGranularity)
	{
		if(parentGranularity > 0 && childGranularity > 0) // check for valid granularity
		{
			m_PGranularity = parentGranularity;
			m_CGranularity = childGranularity;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	@Override
	public int getAmountOfEquipment() {
		
		//check for valid data
		if(m_child == null || m_parent == null ||
				m_PGranularity == 0 || m_CGranularity == 0)
		{
			return 0;
		}		
		else
		{		
			double rate = (double) m_PGranularity / (double) m_CGranularity;
			int amountOfEq = (int)(Math.ceil((m_child.getAmountOfEquipment() * rate)));
			return amountOfEq;
		}
	}

	@Override
	public ECMNEnum getObjectType() {
		return (ECMNEnum.CONNECTOR);
	}
}
